package dongtai;

public class jiuyiba {
    public static int maxSubarraySumCircular(int[] nums) {
        int n=nums.length;
        int[] f=new int[n+1];
        int[] g=new int[n+1];
        int sum=0;
        for(int i=0;i<nums.length;i++){
            sum+=nums[i];
        }
        f[0]=-0x3f3f3f3f;
        g[0]=0x3f3f3f3f;
        int max=-0x3f3f3f3f;
        int min=0x3f3f3f3f;
        for(int i=1;i<n+1;i++){
            f[i]=Math.max(nums[i-1],f[i-1]+nums[i-1]);
            g[i]=Math.min(nums[i-1],g[i-1]+nums[i-1]);
            if(f[i]>max){
                max=f[i];
            }
            if(g[i]<min){
                min=g[i];
            }
        }
        if(min==sum){
            return max;
        }
        return Math.max(max,sum-min);

    }

    public static void main(String[] args) {
       int[] nums ={-3,-2,-3};
        int i = maxSubarraySumCircular(nums);
        System.out.println(i);


    }
}
